package com.xiong.wuliao;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.xiong.wuliao.util.FragmentMainUtil;

/**
 * 主函数
 */
public class MainActivity extends AppCompatActivity {

    private ViewPager myViewPager; // 放置四个Fragment的ViewPager
    private RadioGroup myRadioGroup; // 下排四个Fragment名字的RG

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }

    private void init() {
        findView(); // 初始化控件
        setMyAdapter(); // 设置适配器
        setListener(); // 设置监听事件

    }

    /**
     * 设置适配器
     */
    private void setMyAdapter() {
        myViewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
    }

    /**
     * 设置监听事件
     */
    private void setListener() {
        // RadioButton的点击选择事件
        myRadioGroup.setOnCheckedChangeListener(new MyCheckedChangeListener());
        // ViewPager的滑动事件
        myViewPager.addOnPageChangeListener(new MyPageChangeListener());
        // 避免ViewPager切换时界面重新加载
        myViewPager.setOffscreenPageLimit(3);
    }

    /**
     * 初始化控件
     */
    private void findView() {
        myViewPager = (ViewPager) findViewById(R.id.myViewPagerMain);
        myRadioGroup = (RadioGroup) findViewById(R.id.myRadioGroup);
    }

    /**
     * ViewPager的适配器
     */
    private class MyAdapter extends FragmentPagerAdapter {
        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return FragmentMainUtil.getFragments().get(position);
        }

        @Override
        public int getCount() {
            return FragmentMainUtil.getFragmentSize();
        }
    }

    /**
     * RadioButton的选择事件
     */
    private class MyCheckedChangeListener implements RadioGroup.OnCheckedChangeListener {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            for (int index = 0; index < group.getChildCount(); index++) {
                RadioButton rb = (RadioButton) group.getChildAt(index);
                if (rb.isChecked()) {
                    myViewPager.setCurrentItem(index);
                }
            }
        }
    }

    /**
     * ViewPager的滑动事件
     */
    private class MyPageChangeListener implements ViewPager.OnPageChangeListener {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            RadioButton rb = (RadioButton) myRadioGroup.getChildAt(position);
            rb.setChecked(true);
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }
}
